package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp05Pricing;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class PricingByFilterQuery implements CustomExpressionFactory {

        /**
         * Busca los Prcing segun los filtros  supply , mas los rangos de fechas que fueron indicados
         */
        public Expression createExpression(ExpressionBuilder builder,
                                           Object[] parameters) {
            Expression exp = null;

            Teesp05Pricing supply = (Teesp05Pricing) parameters[0];
            Timestamp EffDateInStart = (Timestamp) parameters[1];
            Timestamp EffDateInEnd = (Timestamp) parameters[2];
            Timestamp EffDateOutStart = (Timestamp) parameters[3];
            Timestamp EffDateOutEnd = (Timestamp) parameters[4];
            Teesp01Part part = supply.getTeesp04Supply().getTeesp01Part();
            String prefix = part.getEesp01PartPrefixCodeC();
            String base = part.getEesp01PartBaseCodeC();
            String suffix = part.getEesp01PartSuffixCodeC();
            String partClass = part.getEesp01PartClassCodeC();
            Teesg01Plant plant = supply.getEesp05PlantCodeC();

            String supplierClass= supply.getTeesp04Supply().getTeess01Supplier().getEess01SupplierClassCodeC();
            String supplierCode= supply.getTeesp04Supply().getTeess01Supplier().getEess01SupplierCodeC();
            exp = builder.get("eesp05PlantCodeC").equal(plant);
            if(!supplierClass.equalsIgnoreCase("-1"))
                exp = exp.and(builder.get("teesp04Supply").get("teess01Supplier").get("eess01SupplierClassCodeC").equal(supplierClass));
            if(supplierCode != null){
                supplierCode += "%";
                exp = exp.and(builder.get("teesp04Supply").get("teess01Supplier").get("eess01SupplierCodeC").like(supplierCode));
            }
            if(!partClass.equalsIgnoreCase("-1"))
                exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartClassCodeC").equal(partClass));
            if (prefix != null) {
                prefix += "%";
                exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartPrefixCodeC").toChar().like(prefix));
            }
            if (base != null) {
                base += "%";
                exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartBaseCodeC").toChar().like(base));
            }
            if (suffix != null) {
                suffix += "%";
                exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartSuffixCodeC").toChar().like(suffix));
            }
            
            if (EffDateInStart!=null){
            exp = exp.and(builder.get("eesp05PriceEffectiveDateY").between(EffDateInStart, EffDateInEnd));
            }
            if (EffDateOutStart!=null){
            exp = exp.and(builder.get("eesp05PriceEndDateY").between(EffDateOutStart,EffDateOutEnd));
            }
        return exp;
    }

}
